package com.uenpay.im.ui.old.activity.personal;

import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.qiyunxin.android.http.HttpInvokeResultCode;
import com.qiyunxin.android.http.listener.OnHttpRequestTextListener;
import com.qiyunxin.android.http.net.HttpInvokeItem;
import com.qiyunxin.android.http.net.HttpRequestAsyncTaskQueue;
import com.qiyunxin.android.http.net.HttpRequestType;
import com.qiyunxin.android.http.net.HttpTextAsyncTask;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.invokeitems.person.UpdatePwdInvokItem;
import com.uenpay.util.app.base.BaseActivity;

/**
 * @author SL
 * @copyRight http://www.qiyunxin.com
 */
public class UpdatePwdActivity extends BaseActivity {

	private EditText old_pwd_edit, new_pwd_edit_one, new_pwd_edit_two;
	private View loading;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		isFullScreen = true;
		super.onCreate(savedInstanceState);
	}

	@Override
	public View bindView() {
		return null;
	}

	@Override
	public int bindLayout() {
		return R.layout.activity_update_pwd_layout;
	}

	@Override
	public void initView(View view) {
		initView();
		initListener();
	}

	protected void initView() {
		((TextView) findViewById(R.id.title_textview)).setText(getResources().getString(R.string.update_pwd));
		((TextView) findViewById(R.id.title_right_tv)).setText(getResources().getString(R.string.save));
		findViewById(R.id.title_right_layout).setVisibility(View.VISIBLE);
		old_pwd_edit = (EditText) findViewById(R.id.old_pwd_edit);
		new_pwd_edit_one = (EditText) findViewById(R.id.new_pwd_edit_one);
		new_pwd_edit_two = (EditText) findViewById(R.id.new_pwd_edit_two);
		loading = findViewById(R.id.loading);
	}

	protected void initListener() {
		findViewById(R.id.title_right_layout).setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				String old_pwd = old_pwd_edit.getText().toString();
				String new_pwd = new_pwd_edit_one.getText().toString();
				String confirmPassword = new_pwd_edit_two.getText().toString();
				if (!TextUtils.isEmpty(old_pwd) && !TextUtils.isEmpty(new_pwd) && !TextUtils.isEmpty(confirmPassword)) {
					if (!checkPwdLength(old_pwd) || !checkPwdLength(new_pwd) || !checkPwdLength(confirmPassword)) {
						IMApplication.showToast(getResources().getString(R.string.pwd_length));
						return;
					} else {
						boolean bl = checkPwd(old_pwd, new_pwd, confirmPassword);
						if (bl) {
							findViewById(R.id.title_right_layout).setEnabled(false);
							updatePwd(old_pwd, new_pwd, confirmPassword);
						}
					}
				} else {
					IMApplication.showToast(getResources().getString(R.string.pwd_not_null));
					return;
				}
			}
		});
		findViewById(R.id.back_layout).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	/**
	 * �޸�����
	 *
	 * @param newPwd
	 * @param confirmPassword
	 */
	private void updatePwd(String oldPwd, String newPwd, String confirmPassword) {
		loading.setVisibility(View.VISIBLE);
		UpdatePwdInvokItem item = new UpdatePwdInvokItem(oldPwd, newPwd, confirmPassword);
		HttpRequestAsyncTaskQueue.getInstance().AddTask(new HttpTextAsyncTask(item).SetRequestType(HttpRequestType.SERVER_ONLY),
				new OnHttpRequestTextListener() {

					@Override
					public void OnFail(boolean isCache, String reason) {
						findViewById(R.id.title_right_layout).setEnabled(true);
						myHandler.sendEmptyMessage(0);
						IMApplication.showToast(reason);
					}

					@Override
					public void OnProgress(long total_bytes, long downloaded_bytes) {

					}

					@Override
					public void OnSuccess(HttpInvokeItem item, boolean isCache, String cacheFilePath) {
						myHandler.sendEmptyMessage(0);
						UpdatePwdInvokItem.UpdatePwdInvokItemResult result = ((UpdatePwdInvokItem) item).getOutput();
						if (result != null && result.status == HttpInvokeResultCode.RESULT_OK) {
							IMApplication.showToast(result.msg);
							UpdatePwdActivity.this.finish();
						} else if (result != null && !TextUtils.isEmpty(result.msg)) {
							findViewById(R.id.title_right_layout).setEnabled(true);
							IMApplication.showToast(result.msg);
						}
					}
				});
	}

	/**
	 * ��֤���볤��
	 *
	 * @param pwd
	 * @return
	 */
	private boolean checkPwdLength(String pwd) {
		if (pwd.length() < 6 || pwd.length() > 16) {
			return false;
		} else {
			return true;
		}
	}

	private Handler myHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			if (msg.what == 0) {
				if (loading != null) {
					loading.setVisibility(View.GONE);
				}
			}
		}

		;
	};

	/**
	 * ��֤����
	 *
	 * @param old_pwd
	 *            ������
	 * @param new_pwd
	 *            ������
	 * @param confirmPassword
	 *            ȷ������
	 * @return false �д���
	 */
	private boolean checkPwd(String old_pwd, String new_pwd, String confirmPassword) {
		if (old_pwd.getBytes().length != old_pwd.length()) {
			IMApplication.showToast(getResources().getString(R.string.old_pwd_can_not_contain_chinese_characters));
			return false;
		}
		if (new_pwd.getBytes().length != new_pwd.length()) {
			IMApplication.showToast(getResources().getString(R.string.new_pwd_can_not_contain_chinese_characters));
			return false;
		}
		if (confirmPassword.getBytes().length != confirmPassword.length()) {
			IMApplication.showToast(getResources().getString(R.string.sure_pwd_can_not_contain_chinese_characters));
			return false;
		}
		if (!new_pwd.equals(confirmPassword)) {
			IMApplication.showToast(getResources().getString(R.string.two_pwd_not_same));
			return false;
		} else if (new_pwd.equals(confirmPassword) && new_pwd.equals(old_pwd)) {
			IMApplication.showToast(getResources().getString(R.string.new_old_pwd_same));
			return false;
		}
		return true;
	}

}
